import { setRecordPath } from '@/services/OsSerivce';
import {
  addVideo,
  loadRecording,
  scanSegmentRecord,
  startRecord,
  stopRecord,
} from '@/services/RecordService';
import { RecordDataType, liveControlRecordType } from '@/types';
import { useModel } from '@@/exports';
import { useInterval } from 'ahooks';
import { useState } from 'react';
import { useRequest } from 'umi';
import { v4 as uuidV4 } from 'uuid';
import { isNotBlank, sleep } from 'web-plugin-utils';

export default () => {
  const [recording, setRecording] = useState<boolean>(false);
  const [recordingPath, setRecordingPath] = useState<string>();

  const [recordVideoId, setRecordVideoId] = useState();

  const { initialState } = useModel('@@initialState');
  const { videoMatrixServer, m7sServerIp, m7sServer, peerServer } =
    useModel('global');
  const { patient } = useModel('patient');
  const [liveControlRecordList, setLiveControlRecordList] = useState<
    liveControlRecordType[]
  >([]);

  const { run: runLoadRecording } = useRequest(
    () => loadRecording(videoMatrixServer),
    {
      manual: true,
      onSuccess(data) {
        setRecording(data);
      },
    },
  );

  useInterval(
    async () => {
      await runLoadRecording();
    },
    isNotBlank(videoMatrixServer) ? 5000 : undefined,
    { immediate: true },
  );

  const { run: runStartRecord } = useRequest<any>(
    (...args: any[]) => startRecord(videoMatrixServer, ...args),
    {
      manual: true,
      debounceInterval: 1000,
    },
  );

  const { run: runStopRecord } = useRequest<any>(
    (...args: any[]) => stopRecord(videoMatrixServer, ...args),
    {
      manual: true,
      debounceInterval: 1000,
    },
  );
  const proxyStartRecord = async (_message: any) => {
    const key = 'proxyStartRecord';
    _message.loading({
      content: '正在启动录制',
      key,
    });

    const uuid = uuidV4();
    const cordingPath = `${patient!.id}-${uuid}`;
    setRecordingPath(cordingPath);
    await setRecordPath(videoMatrixServer, cordingPath);
    let recordData: RecordDataType = {
      fileType: 0,
      fileName: cordingPath,
      fileUrl: `${
        initialState!.miniIOServer
      }/teach-manager-file/matrix/${cordingPath}`,
      uploadType: 1,
      m7sRecordId: cordingPath,
      recordEquipmentIp: m7sServerIp,
      patientId: patient!.id,
      recordType: 3,
    };

    await runStartRecord();
    await sleep(500);
    await runLoadRecording();
    const result = await addVideo(peerServer, recordData);
    if (result.status === 200) {
      setRecordVideoId(result.data);
    }
    _message.destroy(key);
    _message.success('启动成功');
    setRecording(true);
  };

  const proxyStopRecord = async (_message: any) => {
    const key = 'proxyStopRecord';
    _message.loading({
      content: '正在停止录制',
      key,
    });

    await runStopRecord();
    await sleep(500);
    await runLoadRecording();
    setRecordingPath(undefined);
    if (recordVideoId) {
      await scanSegmentRecord(peerServer, recordVideoId);
      setRecordVideoId(undefined);
    }
    _message.destroy(key);
    _message.success('停止成功');
    setRecording(false);
  };

  return {
    recording,
    liveControlRecordList,
    setLiveControlRecordList,
    startRecord: proxyStartRecord,
    stopRecord: proxyStopRecord,
    recordingPath,
  };
};
